-module(calcs).

%%
%% Include files
%%
-include("database.hrl").
-include_lib("stdlib/include/qlc.hrl").
%%
%% Exported Functions
%%

-export([total_money/1, total_value/1, total_profit/1]).

%TODO: complete
%TODO: if buy stock that he have we need to sub the last profid of this stock

%-----------------------------------------------------------------------------------------------------------------%
%---		total_money																								   ---%
%-----------------------------------------------------------------------------------------------------------------%
total_money(UserId)->
	[User]=do(qlc:q([X||X<-mnesia:table(users),
				 X#users.id == UserId])),
	User#users.money.
 		

%-----------------------------------------------------------------------------------------------------------------%
%---			total_value																							   ---%
%-----------------------------------------------------------------------------------------------------------------%
total_value(UserId)->
	[User]=do(qlc:q([X||X<-mnesia:table(users),X#users.id == UserId])),
 	User#users.value.
			

%-----------------------------------------------------------------------------------------------------------------%
%---			total_profit																							   ---%
%-----------------------------------------------------------------------------------------------------------------%
total_profit(UserId)->
	
	[User]=do(qlc:q([X||X<-mnesia:table(users),X#users.id == UserId])),
	User#users.profit.
					
				
	

do(Q) ->
    F = fun() -> qlc:e(Q) end,
    {atomic, Val} = mnesia:transaction(F),
    Val.